import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ;
import javax.swing.* ;
import javax.swing.event.* ;

class ContentPanel extends JPanel {
	Image bgimage = null;

	ContentPanel() {
		MediaTracker mt = new MediaTracker(this);
	    bgimage = Toolkit.getDefaultToolkit().getImage("images/menuMine.png");
	    mt.addImage(bgimage, 0);
	    try {
	      mt.waitForAll();
	    } catch (InterruptedException e) {
	      e.printStackTrace();
	    }
	  }

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
	    Graphics2D g2d = (Graphics2D) g;
	    int x = (this.getWidth() - bgimage.getWidth(null)) / 2;
	    int y = (this.getHeight() - bgimage.getHeight(null)) / 2;
	    g2d.drawImage(bgimage, x, y, null);
	  }
}

public class MineSweeper extends JFrame implements ActionListener ,
		MouseListener {
	
	private static final int port = 4444;

	/* Flag Image Resize */
	ImageIcon flag = new ImageIcon("images/flag.png") ;
	Image flagImg = flag.getImage() ;
	Image newFlagImg = flagImg.getScaledInstance( 20, 20,  java.awt.Image.SCALE_SMOOTH ) ;
	ImageIcon newFlag = new ImageIcon( newFlagImg );
	
	/* Mine Image Resize */
	ImageIcon mine = new ImageIcon("images/mine.png") ;
	Image mineImg = mine.getImage() ;
	Image newMineImg = mineImg.getScaledInstance( 20, 20,  java.awt.Image.SCALE_SMOOTH ) ;
	ImageIcon newMine = new ImageIcon( newMineImg );
	
	/* Game Over image */
	ImageIcon gameOverMine = new ImageIcon("images/gameOver.png") ;
	Image gameOverMineImg = gameOverMine.getImage() ;
	Image newGameOverMineImg = gameOverMineImg.getScaledInstance( 25, 25,  java.awt.Image.SCALE_SMOOTH ) ;
	ImageIcon newGameOverMine = new ImageIcon( newGameOverMineImg );
	
	/* Menu Items */
	JButton localPlay = new JButton ( "Local Play" ) ;
	JFrame menu = new JFrame ( "MAIN MENU" ) ;
	JLabel gameTitle = new JLabel ( "Competitive Minesweeper", newMine, 0 ) ;
	JButton networkPlay = new JButton ( "Network Play" ) ;
	JButton quit = new JButton ( "Quit" ) ;
	
	public void displayMenu ( ) {
		gameTitle.setFont( new Font ( "Serif", Font.BOLD, 20 ) ) ;
		localPlay.setSize ( 200, 200 ) ;
		localPlay.addActionListener ( this ) ;
		networkPlay.setSize ( 200, 200 ) ;
		networkPlay.addActionListener ( this ) ;
		quit.setSize ( 200, 200 ) ;
		quit.addActionListener( this ) ;
		menu.setContentPane( new ContentPanel ( ) ) ;
		Container contentPane = menu.getContentPane();	
		contentPane.setLayout ( new BoxLayout ( contentPane, BoxLayout.Y_AXIS) ) ;
		localPlay.setAlignmentX ( CENTER_ALIGNMENT );
		networkPlay.setAlignmentX ( CENTER_ALIGNMENT );
		quit.setAlignmentX ( CENTER_ALIGNMENT ) ;
		contentPane.add ( gameTitle ) ;
		contentPane.add ( localPlay );
		contentPane.add ( networkPlay ) ;
		contentPane.add ( quit ) ;
		menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//menu.pack ( ) ;
		menu.setSize( 400, 350 ) ;
		menu.setResizable ( false ) ;
		menu.setLocationRelativeTo ( null ) ;
		menu.addWindowListener( null ) ;
		menu.setVisible ( true ) ;
	}

	public MineSweeper ( ) {
			displayMenu ( ) ;
	}
	public static void main ( String [ ] args ) {
		//player = new Player("127.0.0.1", 9001);
		new MineSweeper ( ) ;
	}
	public void actionPerformed ( ActionEvent e ) {
		if ( e.getSource ( ) == localPlay ) {
			new Player();
			menu.dispose ( ) ;
		}
		if ( e.getSource ( ) == networkPlay ) {
			String str = JOptionPane.showInputDialog(null, "IP address as xxx.xxx.xxx.xxx : ", 
					"Enter IP", 1);
			
			new Player(str, port);
			menu.dispose();
		}
		if ( e.getSource ( ) == quit ) {
			menu.dispose ( ) ;
		}
	}
	public void mouseEntered ( MouseEvent e ) {

	}
	public void mouseExited ( MouseEvent e ) {

	}
	public void mousePressed ( MouseEvent e ) {
	
	}
	public void mouseReleased ( MouseEvent e ) {

	}
	public void mouseClicked ( MouseEvent e ) {

	}


}